package usersObjects;

import questionsObjects.*;
import systemMethods.*;
import SmartQuestions.*;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: xpk
 * Date: 11/12/13
 * Time: 5:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserTesting implements UserBehavior {
    private final String descriptionBehavior = "To start the testing";

    @Override
    public String getDescriptionBehavior() {
        return descriptionBehavior;
    }

    @Override
    public boolean executeBehavior(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        int score = 0;
        String userResult;
        Questions storageOfQuestions = currentStorageOfUser.getQuestionsForUser(currentUserCertificate);
        SmartQuestions smartQuestions = new SmartQuestions(storageOfQuestions);

        while (smartQuestions.hasQuestion()) {
            String answers;
            System.out.println(smartQuestions.getQuestion());

            answers = smartQuestions.getAnswer();
            if(answers != null){
            String[] strings = answers.split("[,]");
            for (String str : strings)
                System.out.println(str);
            }

            String str=  TestingSystemUtils.readFromConsole();
            if(str.equalsIgnoreCase("exit")) {
                break;
            }

            if (smartQuestions.isCorrect(str))
                score += 10;
        }
        userResult = String.format("%1$s %2$tB %2$te, %2$tY %3$s %4$d;",
                "Date:", new Date(), " Score:", score);
        System.out.printf("Your score: %d из %d баллов\n", score, 10 * storageOfQuestions.length());
        currentStorageOfUser.setUserScore(currentUserCertificate, userResult);
        currentStorageOfUser.setQuestionHistories(currentUserCertificate, smartQuestions.getQuestionHistory());

        return false;
    }
}
